package com.weavernorth.util;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName GetUserInfo
 * @Description 根据申请人获取年假天数
 * @Author ys951
 * @Date 2019/10/13 22:02
 * @Version 1.0
 */
public class GetUserInfo {

    public static JSONObject getAnnualDayByUserId(String userid) {

        BaseBean base = new BaseBean();
        base.writeLog("userid---" + userid);

        JSONObject json = new JSONObject();
        int annualDay = 0;

        try {

            RecordSet rs = new RecordSet();

            StringBuffer sbf = new StringBuffer();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String currentDate = sdf.format(new Date());
            sbf.append("select annualdays from HrmAnnualManagement where resourceid = ? and annualyear = ?");
            rs.executeQuery(sbf.toString(), userid, currentDate);

            base.writeLog(sbf.toString());
            base.writeLog("userid---" + userid + "-----annualyear---" + currentDate);

            if (rs.next()) {
                // 通过 RecordSet 的 getString 方法查出数据库的数据，annualdays 是数据库的字段
                annualDay = (int)Util.getFloatValue(rs.getString("annualdays"));
            } else {
                annualDay = 9999;
            }

            if (annualDay == 9999) {
                // 未查询到数据
                json.put("status", "001");
            } else {
                // 获取数据正确
                json.put("status", "002");
            }
        } catch (Exception e) {
            base.writeLog("异常--" + e.toString());
        }

        // >=0
        json.put("annualDay", annualDay);
        return json;
    }
}